package tt.dz.appserver.mvc.manage;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.Infomation;
import tt.dz.appserver.service.business.InformationService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;

/**
 * 资讯管理
 * @author USER001
 *
 */

@RestController
@RequestMapping("/manage/pubInformation")
public class InformationManager {
	
	/**资讯管理*/
	@Resource
	InformationService pubInformationService;
	
	/**
	 * 资讯增加
	 **@param infoid		资讯id
	 * @param title			标题	
	 * @param intro			描述
	 * @param url			资讯地址
	 * @param imageurl		图片地址
	 **@param createtime	创建时间
	 * @param remark		说明
	 **@param validflag		有效标志
	 * @return
	 */
	@RequestMapping(value="/PubInformationAdd",produces={"application/json;charset=utf-8"})

	public String pubinformationadd(
		 	@RequestParam(value="title") String title,
		 	@RequestParam(value="intro") String intro,
		 	@RequestParam(value="url") String url,
		 	@RequestParam(value="imageurl") String imageurl,
		 	@RequestParam(value="remark", required=false) String remark) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubInformationService.pubinformationadd(title, intro, url, imageurl, remark);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 资讯删除
	 * @param infoid		资讯id
	 **@param title			标题	
	 **@param intro			描述
	 **@param url			资讯地址
	 **@param imageurl		图片地址
	 **@param createtime	创建时间
	 **@param remark		说明
	 **@param validflag		有效标志
	 * @return
	 */
	@RequestMapping(value="/PubInformationDel",produces={"application/json;charset=utf-8"})

	public String pubinformationdel(
		 	@RequestParam(value="infoid") String infoid) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubInformationService.pubinformationdel(infoid);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 资讯更新
	 * @param infoid		资讯id
	 * @param title			标题	
	 * @param intro			描述
	 * @param url			资讯地址
	 * @param imageurl		图片地址
	 * @param createtime	创建时间
	 * @param remark		说明
	 **@param validflag		有效标志
	 * @return
	 */
	@RequestMapping(value="/PubInformationUpdate",produces={"application/json;charset=utf-8"})

	public String pubinformationupdate(
		 	@RequestParam(value="infoid") String infoid,
		 	@RequestParam(value="title", required=false) String title,
		 	@RequestParam(value="intro", required=false) String intro,
		 	@RequestParam(value="url", required=false) String url,
		 	@RequestParam(value="imageurl", required=false) String imageurl,
		 	@RequestParam(value="remark") String remark) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubInformationService.pubinformationupdate(infoid, title, intro, url,
					imageurl, remark);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 资讯查找
	 **@param infoid		资讯id
	 **@param title			标题	
	 **@param intro			描述
	 **@param url			资讯地址
	 **@param imageurl		图片地址
	 **@param createtime	创建时间
	 **@param remark		说明
	 **@param validflag		有效标志
	 * @return
	 */
	@RequestMapping(value="/PubInformationSelect",produces={"application/json;charset=utf-8"})

	public String pubinformationselect() {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			List<Infomation> resultObject = pubInformationService.pubinformationselect();
			
			String code = Response.SUCCESS.getCode();
			String msg = Response.SUCCESS.getMsg();
			
			resultMappingEntity = Result.responseBody(code, msg, resultObject);
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}

}
